package com.xp.flower.qsd.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.xp.flower.api.IFlowerService;
import com.xp.flower.common.dto.FlowerDTO;
import com.xp.flower.common.dto.UserDTO;
import com.xp.flower.qsd.consts.WebConsts;

@RequestMapping("flowerController")
@Controller
public class FlowerController {

    @Autowired
    private IFlowerService flowerServiceImpl;

    @RequestMapping(value = "goToFlower", method = RequestMethod.GET)
    public String goToFlower(HttpSession session, Model model) {
        FlowerDTO flowerDTO = new FlowerDTO();
        PageInfo<FlowerDTO> flowerPage = flowerServiceImpl.queryFlowersByParams(flowerDTO, 1, 5);
        model.addAttribute("flowerPage", flowerPage);
        return "qsd/guang";
    }
    
    
    @RequestMapping(value = "flowerPage", method = RequestMethod.POST)
    @ResponseBody
    public List<FlowerDTO> flowerPage(HttpSession session, Model model,Integer pageNum ,Integer pageSize) {
        FlowerDTO flowerDTO = new FlowerDTO();
        PageInfo<FlowerDTO> flowerPage = flowerServiceImpl.queryFlowersByParams(flowerDTO, pageNum, pageSize);
        return flowerPage.getList();
    }
    
    
    @RequestMapping(value = "flowerDetail", method = RequestMethod.GET)
    public String flowerDetail(@RequestParam(value = "id", required = true)Long id,HttpSession session,Model model){
        
        UserDTO userDTO = (UserDTO)session.getAttribute(WebConsts.USERINFO);
        Long userId = null;
        if(null != userDTO){
            userId = userDTO.getId();
        }
        
        FlowerDTO flowerDTO = flowerServiceImpl.queryFlowerById(id,userId);
        model.addAttribute("flowerDTO", flowerDTO);
        return "qsd/flowerinfo";
    }
    
    
}
